import React from 'react';
import './index.less';
import {connect} from "dva";
import Curd from "components/Curd";
import createColumns from "./columns";
import searchColumns from "./searchColumns";


@connect(({order, shipper}) => ({order, shipper}))
export default class extends Curd {

  getNamespace() {
    return 'order';
  }

  getTableColumns() {
    return createColumns(this);
  }

  getSearchColumns() {
    return searchColumns;
  }

  getAll = (key) => {

    const {all, loadingAll} = this.getModel(key);

    const dict = (!!all && all.length > 0) ? all.map(v => {
      return ({code: v.code, codeName: v.name})
    }) : undefined;

    return ({loading: loadingAll, dict});
  }

  async addAndModify(value, record) {

    const {shippingName} = value;
    const {all} = this.props.shipper;
    let shipper;
    all.forEach(v =>{
      if (v.code == shippingName)
        shipper = v;
    });

    if (!!shipper){
      value = {orderId: value.orderId, shippingName: shipper.name, shippingNameCode: shipper.code,
        shippingCode: value.shippingCode, shippingNameWebCode: shipper.webCode};
      console.log("addAndModify", value, record);
      this.dispatch({
        type: `${this.getNamespace()}/send`,
        payload: {
          value,
          notice: this.showMsg,
          success: () => {
            this.getPageInfo(this.state);
            this.setState({
              record: null,
              visible: false
            });
          }
        }
      });
    }
  }

  // addAndModify(value, record) {
  //
  //   const {pic} = value;
  //   console.log(value);
  //   putFile("1.png", pic[0].originFileObj);
  // }
}
